package com.mobile.framework.upload.struts1;

import com.mobile.framework.upload.FileType;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import org.apache.struts.upload.FormFile;

import javax.imageio.ImageIO;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.*;

public class UploadFile {
	public int saveFile(FormFile filedate, String filePath) {
		try {
			InputStream stream = filedate.getInputStream();
			OutputStream bos = new FileOutputStream(filePath);
			if (filedate.getFileSize() > 205600) {

				throw new Exception();
			}
			int bytesRead = 0;
			byte[] buffer = new byte[8192];
			while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
				bos.write(buffer, 0, bytesRead);
			}
			bos.close();
			stream.close();
			return 1;
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}

	public static boolean saveFiles(FormFile filedata, String filePath,long maxSize)
			throws Exception {
		boolean flag = false;
		// ��ͼƬ�ϴ�
		FileOutputStream fos = new FileOutputStream(filePath);
		if (filedata.getFileSize() > maxSize ) {
			throw new Exception("上传的文件太大");
		}
		byte b[] = filedata.getFileData();
		fos.write(b);
		fos.flush();
		fos.close();
		flag = true;
		return flag;
	}

	public static boolean saveFiles(FormFile filedata, String filePath)
			throws Exception {
		boolean flag = false;
		// ��ͼƬ�ϴ�
		FileOutputStream fos = new FileOutputStream(filePath);
		if (filedata.getFileSize() >2048000) {
			throw new Exception("上传的文件太大");
		}
		byte b[] = filedata.getFileData();
		fos.write(b);
		fos.flush();
		fos.close();
		flag = true;
		return flag;
	}

	/**
	 * 按指定大小创建图片,并返回创建的文件名称
	 *
	 * @param imgfile
	 * @param filePath
	 * @param width
	 * @param height
	 * @return fileName
	 * @author zchang
	 * @date 2008-4-28
	 * @throws Exception
	 */
	public static String createPhoto(FormFile imgfile, String filePath,
			int width, int height) throws Exception {

		String fileName = null;// ��ɵ��ļ���

		FileType f = new FileType();

		// �ж��ǲ���ͼƬ�ļ�
		if (!f.isSameFileType(imgfile.getFileName()))
			throw new Exception(imgfile + "不是图片文件");

		String type = imgfile.getFileName().substring(
				imgfile.getFileName().lastIndexOf(".") + 1,
				imgfile.getFileName().length());

		// �����ж��ϴ���ͼƬ��gif����JPG ImageIOֻ�ܽ�gifת��Ϊpng
		// if (f.isSameFileType(imgfile.getFileName(), "jpg")) {
		// ext = "jpg";
		// } else {
		// ext = "png";
		// }

		// �Զ�����ļ����
		fileName = FileType.autoOrderNum() + "." + type;
		File fo = new File(filePath, fileName);
		int nw = width;
		int nh = height;
		AffineTransform transform = new AffineTransform();
		BufferedImage bis = ImageIO.read(imgfile.getInputStream());
		int w = bis.getWidth();
		int h = bis.getHeight();
		double sh = (double) w / h;
		double sw = (double) h / w;
		if (w <= nw)
			nw = w;
		if (h <= nh)
			nh = h;
		if (w > nw)
			nh = (int) (sw * nw);
		if (h > nh)
			nw = (int) (sh * nh);

		BufferedImage tag = new BufferedImage(nw, nh,
				BufferedImage.TYPE_INT_RGB);
		tag.getGraphics().drawImage(bis, 0, 0, nw, nh, null); // ������С���ͼ
		FileOutputStream out = new FileOutputStream(fo); // ����ļ���
		JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
		encoder.encode(tag); // ��JPEG����
		// System.out.print(width+"*"+height);
		out.close();

		// double sx = (double) nw / w;
		// double sy = (double) nh / h;
		// transform.setToScale(sx, sy);
		//
		// System.out.println(w + " " + h);
		// System.out.println(nw + " " + nh);
		// AffineTransformOp ato = new AffineTransformOp(transform,
		// AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
		// BufferedImage bid = new BufferedImage(nw, nh,
		// BufferedImage.TYPE_3BYTE_BGR);
		// ato.filter(bis, bid);
		// ImageIO.write(bis, type, fo);
		return fileName;
	}

}
